package com.mming.sso.filter;

import com.mming.sso.util.TimeUtil;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class BaseParamsCheckFilter extends BaseFilter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String strTimestamp = request.getParameter("t");

        if (strTimestamp == null)
            outputResponse(400, response);
        else if (Long.parseLong(strTimestamp) < TimeUtil.getTimestamp10() - 10 ||
                Long.parseLong(strTimestamp) > TimeUtil.getTimestamp10() + 10)
            outputResponse(403, response);
        else
            filterChain.doFilter(servletRequest, servletResponse);
    }
}
